<?php
/**
 * Class UserReceive
 * Desc
 * Create on 2021/10/18 11:52
 * Create by wangyafang
 */

namespace app\api\controller\xyb;


use addons\xyb\XypHelper;
use app\api\model\xyb\Profit;
use app\api\model\xyb\Wallets;
use app\api\model\xyb\Order;
use app\api\service\UsersService;
use app\common\model\xyb\AdminBelong;
use think\Db;
use app\api\model\xyb\OrderReceive;

class UserReceive extends Base
{
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\api\model\xyb\UserReceive();
    }

    /**
     * desc:接单员个人中心
     */
    public function center()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $users = $this->getAuthUsers();

        $walletsModel = new Wallets();
        $walletsInfo = $walletsModel->where(['user_id'=>$users->id, 'type'=>'1'])->where('status', 'normal')->find();
        $profitModel = new Profit();
        $where['receive_id'] = array('eq', $users->id);
       // $where['agent_id'] = $param['agent_id'];
        if(!$walletsInfo) {
            $total_income = 0.00;
            $can_withdrawal_amount = 0.00;
        } else {
            $total_income = $walletsInfo['total_income'];
            $can_withdrawal_amount = $walletsInfo['can_withdrawal_amount'];
        }
        $orderModel = new Order();
        $data = [
            'total_income'          => $total_income,
            'finish_order_count'    => $orderModel->where($where)->where('status','in',[2,3,7,8])->count(),
            'bad_order_count'       => 0,
            'agent_income'          => $profitModel->where($where)->sum('agent_profit'),
            'today_income'=>$profitModel->where($where)->whereTime('createtime', 'today')->sum('receive_profit'),
            'can_withdrawal_amount'=>$can_withdrawal_amount,
            'in_progress_order_count'=>$orderModel->where($where)->where('status','in',[2,7])->count(),

            'week_income'=>$profitModel->where($where)->whereTime('createtime', 'week')->sum('receive_profit'),
            'week_in_progress_order_count'=>$orderModel->where($where)->where('status','in',[2,7])->whereTime('createtime', 'week')->count(),
            'week_finish_order_count'=>$orderModel->where($where)->where('status',3)->whereTime('createtime', 'week')->count(),
            'month_income'=>$profitModel->where($where)->whereTime('createtime', 'month')->sum('receive_profit'),
            'month_in_progress_order_count'=>$orderModel->where($where)->where('status','in',[2,7])->whereTime('createtime', 'month')->count(),
            'month_finish_order_count'=>$orderModel->where($where)->where('status',3)->whereTime('createtime', 'month')->count(),
            'year_income'=>$profitModel->where($where)->whereTime('createtime', 'year')->sum('receive_profit'),
            'year_in_progress_order_count'=>$orderModel->where($where)->where('status','in',[2,7])->whereTime('createtime', 'year')->count(),
            'year_finish_order_count'=>$orderModel->where($where)->where('status',3)->whereTime('createtime', 'year')->count(),
        ];
        $this->success('请求成功', $data);
    }

    /**
     * desc:收入金额列表
     */
    public function profit_list()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $profitModel = new Profit();
        $user_id = $this->auth->id;
        $where['receive_id'] = array('eq',$user_id);
        $where['app_plat'] = array('eq','MP-WEIXIN');
        $data = $profitModel->getList($profitModel,$where,'desc,createtime,receive_profit','id desc');
        $this->success('请求成功',$data);
    }

    public function can_withdrawal_amount()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $walletsObj = (new Wallets())->where([
            'type'=>1,
            'user_id'=>$this->auth->id,
        ])->find();
        if(!$walletsObj) {
            $walletsObj['can_withdrawal_amount'] = 0.00;
        }
       // $service_charge = UsersService::getAgentConfig(); $this->configObj->service_charge;
        $this->success('请求成功',['amount'=>$walletsObj['can_withdrawal_amount'],'service_charge'=>0]);
    }

    /**
     * desc:跑腿员订单列表
     */
    public function order_list()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $user_id = $this->auth->id;
        $param = $this->request->param();
        $where['agent_id'] = $param['agent_id'];
        $where['pay'] = 1;
        $orderReceiveModel = new OrderReceive();
        switch ($param['receive_status']) {
            case 1://未接单
                $where['user_id'] = array('neq',$this->auth->id);
                $where['status'] = 1;
                break;
            case 2://进行中
                $where['status'] = array('in',[2,7]);
                $order_id_array = $orderReceiveModel->where([
                    'receive_id'=>$user_id,
                    'app_plat'=>$param['app_plat'],
                ])->column('order_id');
                if(empty($order_id_array)) {
                    $this->success('请求成功',['total'=>0,'data'=>[]]);
                }
                $where['id'] = array('in',$order_id_array);
                break;
            case 3://已完成
                $where['status'] = 3;
                $order_id_array = $orderReceiveModel->where([
                    'receive_id'=>$user_id,
                    'app_plat'=>$param['app_plat'],
                ])->column('order_id');
                if(empty($order_id_array)) {
                    $this->success('请求成功',['total'=>0,'data'=>[]]);
                }
                $where['id'] = array('in',$order_id_array);
                break;
            case 4://已取消
                $order_id_array = $orderReceiveModel->where([
                    'receive_id'=>$user_id,
                    'app_plat'=>$param['app_plat'],
                    'status'=>4
                ])->column('order_id');
                if(empty($order_id_array)) {
                    $this->success('请求成功',['total'=>0,'data'=>[]]);
                }
                $where['id'] = array('in',$order_id_array);
                break;
        }
        $orderModel = new Order();
        $count = $orderModel->where($where)->count();
        $serverController = new Server();
        $data = $orderModel->where($where)
            ->alias('order')
            ->order('id desc')
            ->page($param['page'],$param['limit'])
            ->with(['user'])
            ->field("order.id,order.information,order.start,order.destination,order.hope_arrive,order.express,
            game_platform,game_time,receive_limit,
            lease_duration,
            total_fee,createtime,type,user_id,status,agent_id,total_fee as price")//total_fee as price订单金额
            ->select();
        $app_plat = $this->request->param('app_plat');
        foreach ($data as $k=>$v) {
            unset($data[$k]['down_method_text']);
            $data[$k]['time'] = XypHelper::xyb_dateline($v['createtime']);
            $data[$k]['total_fee'] = $serverController->get_receive_profit($v);//预估收益
            switch ($app_plat) {
                case 'MP-WEIXIN':
                    $wechatInfo = Db::name('xyb_user_plat')
                        ->alias('u_p')
                        ->where('u_p.user_id',$v['user_id'])
                        ->join('xyb_user_wechat u_w','u_w.id = u_p.wechat_id')
                        ->field('u_w.nickname,u_w.avatar')
                        ->find();
                    $data[$k]['nickname'] = $wechatInfo['nickname'];
                    $data[$k]['avatar'] = XypHelper::xyb_full_url($wechatInfo['avatar']);
                    break;
            }
            if($v['type'] == 2) {
                $data[$k]['information'] = '';
            }
            if($param['receive_status'] == 4) {
                $data[$k]['status'] = 4;
            }
        }
        $this->success('请求成功', ['total'=>$count,'data'=>$data]);
    }

}